﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UniversityManagementSystem.DAL;
using System.Data.SqlClient;
using System.Data;

namespace UniversityManagementSystem.Gateway
{
    [Serializable]
    public class BuildingGateway : GatewayConnection
    {
        private string commandString = null;
        [NonSerialized]
        private SqlDataReader getData = null;

        public List<Building> GetAllBuildings()
        {
            List<Building> getBuildings = new List<Building>();
            try
            {
                GetSqlConn.Open();
                commandString = string.Format("SELECT * FROM t_buildings");
                GetSqlCommand.CommandText = commandString;
                getData = GetSqlCommand.ExecuteReader();
                while (getData.Read())
                {
                    Building aBuilding = new Building();
                    aBuilding.BuildingId = getData["building_id"].ToString();
                    aBuilding.BuildingName = getData["building_name"].ToString();
                    getBuildings.Add(aBuilding);
                }
                GetSqlConn.Close();
            }

            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                if ((GetSqlConn != null) && (GetSqlConn.State == ConnectionState.Open))
                    GetSqlConn.Close();
            }
            return getBuildings;
        }

        public List<Building> GetAllRooms(string buildingId)
        {
            List<Building> getRooms = new List<Building>();
            try
            {
                GetSqlConn.Open();
                commandString = string.Format("SELECT * FROM t_rooms WHERE building_id = @buildingId");
                GetSqlCommand.CommandText = commandString;
                GetSqlCommand.Parameters.Clear();
                GetSqlCommand.Parameters.Add("@buildingId", SqlDbType.VarChar);
                GetSqlCommand.Parameters["@buildingId"].Value = buildingId;
                getData = GetSqlCommand.ExecuteReader();
                while (getData.Read())
                {
                    Building aBuilding = new Building();
                    aBuilding.RoomNo = getData["room_no"].ToString();
                    getRooms.Add(aBuilding);
                }
                GetSqlConn.Close();
            }

            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                if ((GetSqlConn != null) && (GetSqlConn.State == ConnectionState.Open))
                    GetSqlConn.Close();
            }
            return getRooms;
        }
    }
}